<script setup lang="ts">
/* eslint-disable*/
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
//导入API
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from "../../services/home";
//导入类型
import type { BannerItem, CategoryItem, HotItem } from "@/types/home";
import type { XtxGuessInstance } from "@/types/component";

//导入首页子组件
import CustomNavbar from "./components/CustomNavbar.vue"; //导航
import CategoryPanel from "./components/CategoryPanel.vue"; //分类
import HotPanel from "./components/HotPanel.vue"; //推荐
import PageSkeleton from "./components/PageSkeleton.vue"; //骨架屏

//获取轮播图数据
const bannerList = ref<BannerItem[]>([]);
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI(1);
  bannerList.value = res.result;
  console.log(res);
};
//获取分类数据
const homeCategorList = ref<CategoryItem[]>([]);
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI();
  homeCategorList.value = res.result;
  console.log(res);
};
//获取分类数据
const hotPanelList = ref<HotItem[]>([]);
const getHotPanelData = async () => {
  const res = await getHomeHotAPI();
  hotPanelList.value = res.result;
  console.log(res);
};

//猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>();
//触底函数
const onScrolltolower = () => {
  guessRef.value?.getMore();
};
// 下拉刷新状态
const isTriggered = ref(false);
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开启动画
  isTriggered.value = true;
  console.log("刷新");
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData();
  // 加载数据
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotPanelData()]);
  //关闭动画;
  isTriggered.value = false;
};
//数据是否加载完毕
const isLoading = ref(false);
onLoad(async () => {
  isLoading.value = true;
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHotPanelData()]);
  isLoading.value = false;
});
//引入组件
</script>

<template>
  <CustomNavbar />
  <!-- 滚动 -->
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrolltolower"
    refresher-enabled
    :refresher-triggered="isTriggered"
    @refresherrefresh="onRefresherrefresh"
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <XtxSwiper :list="bannerList" />
      <CategoryPanel :list="homeCategorList" />
      <HotPanel :list="hotPanelList" />
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
  .scroll-view {
    flex: 1;
  }
}
</style>
